package cn.bearspark.forward.provider.dao.mapper;

import cn.bearspark.framework.redis.starter.buffer.list.RedisPageListMapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 动态转发的数据映射对象
 *
 * @author f
 */
@Mapper
public interface HappeningFwdMapper extends RedisPageListMapper {

    /**
     * 添加转发动态的记录
     *
     * @param userId      转发动态的用户 id
     * @param happeningId 被转发的动态 id
     */
    @Insert("INSERT INTO t_happening_forward (user_id, happening_id) VALUES (#{userId}, #{happeningId})")
    void insert(long userId, long happeningId);

    /**
     * 查询转发指定动态的用户 id 集合，按照时间倒序排序
     *
     * @param happeningId 指定动态的 id
     * @param offset      偏移量
     * @param size        元素数
     * @return 转发指定动态的用户 id 集合
     */
    @Override
    @Select("SELECT user_id FROM t_happening_forward WHERE happening_id = #{happeningId} " +
            "ORDER BY create_time DESC LIMIT #{offset}, #{size}")
    List<Long> listPageId(long happeningId, int offset, int size);

    /**
     * 统计指定用户转发的指定动态的条数
     *
     * @param userId      指定的用户 id
     * @param happeningId 指定的动态 id
     * @return 指定用户转发的指定动态的条数
     */
    @Select("SELECT COUNT(*) FROM t_happening_forward " +
            "WHERE user_id = #{userId} AND happening_id = #{happeningId}")
    int count(long userId, long happeningId);
}
